package com.jinzhu.service.impl;


import com.jinzhu.bo.SubmitOrderBO;
import com.jinzhu.mapper.OrderItemsMapper;
import com.jinzhu.mapper.OrderStatusMapper;
import com.jinzhu.mapper.OrdersMapper;
import com.jinzhu.pojo.Orders;
import com.jinzhu.pojo.UserAddress;
import com.jinzhu.service.AddressService;
import com.jinzhu.service.ItemsService;
import com.jinzhu.service.OrderService;
import com.jinzhu.utils.YesOrNo;
import com.jinzhu.vo.OrderVO;
import org.n3r.idworker.Sid;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.List;

@Service
public class OrderServiceImpl implements OrderService {

    @Autowired
    private OrdersMapper ordersMapper;

    @Autowired
    private OrderItemsMapper orderItemsMapper;

    @Autowired
    private OrderStatusMapper orderStatusMapper;

    @Autowired
    private AddressService addressService;

    @Autowired
    private ItemsService itemService;

    @Autowired
    private Sid sid;


    /**
     * Propagation.REQUIRED
     * 如果当前存在事务，则加入该事务；如果当前没有事务，则创建一个新的事务。这是默认值。
     * 创建用户订单
     * @param submitOrderBO
     * @return
     */
    @Transactional(propagation = Propagation.REQUIRED)
    @Override
    public OrderVO createOrder(SubmitOrderBO submitOrderBO) {
        String userId = submitOrderBO.getUserId();
        String addressId = submitOrderBO.getAddressId();
        String itemSpecIds = submitOrderBO.getItemSpecIds();
        Integer payMethod = submitOrderBO.getPayMethod();
        String leftMsg = submitOrderBO.getLeftMsg();
        // 包邮费用设置为0
        Integer postAmount = 0;

        String orderId = sid.nextShort();

        //根据用户id和地址id，查询具体的用户地址对象信息
        UserAddress address = addressService.queryUserAddres(userId, addressId);

        // 1. 新订单数据保存
        Orders newOrder = new Orders();
        newOrder.setId(orderId);
        newOrder.setUserId(userId);

        newOrder.setReceiverName(address.getReceiver());
        newOrder.setReceiverMobile(address.getMobile());
        newOrder.setReceiverAddress(address.getProvince() + " "
                + address.getCity() + " "
                + address.getDistrict() + " "
                + address.getDetail());

//        newOrder.setTotalAmount();
//        newOrder.setRealPayAmount();
        newOrder.setPostAmount(postAmount);

        newOrder.setPayMethod(payMethod);
        newOrder.setLeftMsg(leftMsg);

        newOrder.setIsComment(YesOrNo.NO.type);
        newOrder.setIsDelete(YesOrNo.NO.type);
        newOrder.setCreatedTime(new Date());
        newOrder.setUpdatedTime(new Date());

        return null;

    }

    public static void main(String[] args) {
        int i =155/3;
        System.out.println(i);
    }
}
